package com.wuhongyu.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.wuhongyu.entity.Message;

public class MessageDao {
	private String url ="jdbc:mysql://localhost:3306/test";
    private String driver = "com.mysql.jdbc.Driver";
    private String name = "root";
    private String pwd = "root";

    public Connection getConnection() throws Exception {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, name, pwd);
        return connection;
    }

  


    public void add(Message message){
        Connection connection = null;
        PreparedStatement ps = null;
        String sql = "insert into message (post,rate,company,salary,address,date)values(?,?,?,?,?,?);";
        try {
            connection = this.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1,message.getPost());
            ps.setString(2,message.getRate());
            ps.setString(3,message.getCompany());
            ps.setString(4,message.getSalary());
            ps.setString(5,message.getAddress());
            ps.setString(6,message.getDate());
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ps !=null) {
                try {
                    ps.close();
                    //System.out.println("关闭s");
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        try {
                            connection.close();
                            //System.out.println("关闭c");
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
	
}
